package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.PoiMapper;
import com.itheima.reggie.service.PoiService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


@Service
public class PoiServiceImpl implements PoiService {

    @Autowired(required = false)
    private PoiMapper poiMapper;

    @Override
    public void derive() throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("员工信息表");
        Row row = sheet.createRow(0);

        row.createCell(0).setCellValue("用户id");
        row.createCell(1).setCellValue("登录的时候使用用户名");
        row.createCell(2).setCellValue("昵称");
        row.createCell(3).setCellValue("手机号");
        row.createCell(4).setCellValue("性别");
        row.createCell(5).setCellValue("用户的状态");




        List<Employee> employeeList = poiMapper.derive();
        for (int i = 0; i < employeeList.size(); i++) {

            row =  sheet.createRow(i+1);

            Cell cell = row.createCell(0);
            cell.setCellValue(employeeList.get(i).getId());

            cell = row.createCell(1);
            cell.setCellValue(employeeList.get(i).getUsername());

            cell = row.createCell(2);
            cell.setCellValue(employeeList.get(i).getName());

            cell = row.createCell(3);
            cell.setCellValue(employeeList.get(i).getPhone());

            cell = row.createCell(4);
            if ("1".equals(employeeList.get(i).getSex())){
                cell.setCellValue("男");
            }else {
                cell.setCellValue("女");
            }


            cell = row.createCell(5);
            cell.setCellValue(employeeList.get(i).getStatus());


        }

        workbook.write(new FileOutputStream("D:/employee.xlsx"));

    }
}
